import 'package:flutter/material.dart';
import 'package:flutter_massage/common_lib/extension/ex_widget.dart';
import 'package:flutter_massage/common_lib/index.dart';
import 'package:flutter_massage/common_lib/routers/route.dart';
import 'package:flutter_massage/pages/my/address/address_bean.dart';
import 'package:flutter_massage/pages/my/address/my_address_page.dart';
import 'package:flutter_massage/pages/technician/choose_address/edit_address_page.dart';
import 'package:flutter_massage/sqlite/db_manager.dart';
import 'package:flutter_massage/values/index.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';

class ChooseAddressPage extends GetView<ChooseAddressLogic> {
  const ChooseAddressPage({required this.onChoose, super.key});

  final ValueChanged<AddressBean> onChoose;

  @override
  Widget build(BuildContext context) {
    return BaseScaffold(
      titleStr: '选择地址',
      child: GetBuilder<ChooseAddressLogic>(
        init: ChooseAddressLogic(),
        assignId: true,
        builder: (logic) {
          return Stack(
            alignment: Alignment.bottomCenter,
            children: [
              ListView.separated(
                padding: EdgeInsets.only(bottom: ScreenUtil().bottomBarHeight + 60.w),
                itemCount: logic.list.length,
                separatorBuilder: (BuildContext context, int index) => CommonDivider(
                  marginLeft: 12.w,
                  marginRight: 12.w,
                ),
                itemBuilder: (BuildContext context, int index) {
                  return AddressItem(
                    logic.list[index],
                    onTap: () {
                      onChoose(logic.list[index]);
                      CommonRoute.close();
                    },
                    onDelete: () => logic.deleteAddress(logic.list[index]),
                  );
                },
              ),
              ButtonWidget.text(
                '添加地址',
                onTap: () => CommonRoute.open(const EditAddressPage()),
                hasInkWell: false,
                alignment: Alignment.center,
                height: 50.w,
                gradient: CommonDecoration.xydGradient,
                borderRadius: 50.w,
                fontSize: 17,
                fontWeight: FontWeight.bold,
                textColor: AppColors.white,
              ).positioned(left: 12.w, right: 12.w, bottom: ScreenUtil().bottomBarHeight),
            ],
          );
        },
      ),
    );
  }
}

class ChooseAddressLogic extends GetxController {
  List<AddressBean> list = [];

  @override
  void onReady() async {
    super.onReady();
    CommonObservable().addObserver(EditAddressLogic.keyListener, refreshList);
    getAddressList();
  }

  @override
  void onClose() {
    super.onClose();
    CommonObservable().removeObserver(EditAddressLogic.keyListener, refreshList);
  }

  void refreshList(key, [value]) => getAddressList();

  void getAddressList() async {
    list = await DbManager.getAddressList();
    update();
  }

  void deleteAddress(AddressBean bean) async {
    if (bean.addressId.empty) return;
    await DbManager.deleteAddress(bean.addressId!);
    getAddressList();
  }
}
